//
//  LLTabBar2.m
//  Weibo
//
//  Created by 李灵斌 on 15-1-28.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "LLTabBar2.h"
#import "LLTabbarButton.h"
@interface LLTabBar2()

@property (nonatomic, strong) NSMutableArray *btns;
@property (nonatomic, weak) UIButton *button;
@property (nonatomic, weak) UIButton *selectedBtn;
@end

@implementation LLTabBar2

- (NSMutableArray *)btns{
    
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    
    if (self = [super initWithCoder:aDecoder]) {
        
        [self setup];
    }
    return self;
}

- (void)setup{
    
    self.backgroundColor = LLColor(246, 246, 246);
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    [self addSubview:button];
    self.button = button;
    
    [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}

/**
 *  中间大的按钮点击事件
 */
- (void)btnClick{
    
    // 点击中间的按钮要modal一个控制器，而modal只有控制器能完成，因此可以考虑代理交给控制器来处理
    if ([self.delegate respondsToSelector:@selector(tabBar2:btnClick:)]) {
        
        [self.delegate tabBar2:self btnClick:self.button];
    }
}

- (void)addItem:(UITabBarItem *)item{
    
    LLTabbarButton *btn = [LLTabbarButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:item.image forState:UIControlStateNormal];
    [btn setImage:item.selectedImage forState:UIControlStateSelected];
    [btn setTitle:item.title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:10];
    [self.btns addObject:btn];
    [self addSubview:btn];
    btn.tag = self.btns.count - 1;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    if (self.btns.count == 1) {
        [self btnClick:btn];
    }
}

- (void)btnClick:(LLTabbarButton *)btn{
    
    if ([self.delegate respondsToSelector:@selector(tabBar2:form:to:)]) {
        
        [self.delegate tabBar2:self form:self.selectedBtn.tag to:btn.tag];
    }
    
    self.selectedBtn.selected = NO;
    btn.selected = YES;
    self.selectedBtn = btn;
    
//    [UIView animateWithDuration:0.2 animations:^{
//        // 缩小
//        // sx / sy 如果是1代表不缩放, 如果大于1代表放大 如果小于1代表缩小
//        btn.transform = CGAffineTransformMakeScale(0.4, 0.4);
//        
//    } completion:^(BOOL finished) {
//        
//        [UIView animateWithDuration:0.1 animations:^{
//            // 放大
//            btn.transform = CGAffineTransformMakeScale(1.4, 1.4);
//            
//        } completion:^(BOOL finished) {
//            
//            [UIView animateWithDuration:0.1 animations:^{
//                // 还原
//                btn.transform = CGAffineTransformIdentity;
//            }];
//        }];
//    }];
}

/**
 *  布局子控件
 */
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    NSUInteger count = self.btns.count;
    NSUInteger subviewsCount = self.subviews.count;
    
    CGFloat btnW = self.width / subviewsCount;
    CGFloat btnH = self.height;
    for (int i = 0; i<count; i++) {
        
        LLTabbarButton *btn = self.btns[i];
        btn.x = i * btnW;
        btn.width = btnW;
        btn.height = btnH;
        if (i >=2) {
            btn.x = (i + 1) * btnW;
        }
    }
    // 设置中间按钮的frame
    self.button.centerX = self.width * 0.5;
    self.button.centerY = self.height * 0.5;
    self.button.size = self.button.currentBackgroundImage.size;

}

@end
